<?php

include_once APPLICATION_DIR . "/application/model/SERVICE_BOOKING.php";

class Service_Booking extends DataObjects_SERVICE_BOOKING
{
    public function validateInput(){
        global $error_msg;

        return $error_msg->isNoError();
    }

   public function canBeDeleted($id){
	   $booking = new Booking_detail_service();
       $booking->ID_SERVICE_BOOKING = $id;
       $booking->find();
       if($booking->N == 0)
           return true;
       else
           return false;
   }
   public static function getWorkingHours($date){
	   $day = date("w",strtotime(date_to_date_sql($date)));
	   $start = 10;
	   $end = 19;
	   if($day == 0){
			$start = 11;
			$end = 17;
	   }
	   elseif($day == 6){
			$start = 10;
			$end = 18;
	   }
	   $str = '<select name="TIME" id="TIME" onchange="getAvailableTechnician()">';
	   for($i=$start;$i<=$end;$i++){
		   $str .= '<option value="'.($i*2) .'">'.$i.':00</option>';
		   $str .= '<option value="'. (($i*2) + 1) .'">'.$i.':30</option>';
	   }
	   $str .= '</select>';
	   return $str;
   }
   public function parseService2Array(){
	   $query = "SELECT SB.*, SG.TITLE TITLE_GROUP FROM SERVICE_GROUP SG "
        . "LEFT JOIN SERVICE_BOOKING SB ON SB.ID_SERVICE_GROUP = SG.ID "
		. "WHERE SB.DISABLED = 0 AND SG.DISABLED = 0 "
        . "ORDER BY SG.POS, SG.TITLE,SB.POS, SB.TITLE ASC";
		$this->query($query);
		$serviceData = array();
		while($this->fetch()){
			if(!isset($serviceData[$this->ID_SERVICE_GROUP])){
				$serviceData[$this->ID_SERVICE_GROUP] = array("TITLE"=>$this->TITLE_GROUP,"DATA"=>array());
			}
			$serviceData[$this->ID_SERVICE_GROUP]["DATA"][$this->ID] = array(
				"ID"=>$this->ID,
				"TITLE"=>$this->TITLE,
				"TIME"=>$this->TIME,
				"PRICE"=>$this->PRICE
			);
		}
		return $serviceData;
   }
}
?>
